﻿/*
 * marqdown
 * --------
 * a C# Markdown processor
 * 
 * Copyright (c) 2010 Andrew Wall
 * http://code.google.com/p/marqdown/
 * 
 * 
 */


#region Copyright and license

/*

Copyright (c) 2010 Andrew Wall

 http://www.opensource.org/licenses/mit-license.php
  
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#endregion


using System;
using System.Collections.Generic;

namespace Marqdown
{
    namespace StateMachine
    {
        public class UnorderedList : CompetitiveState
        {
            internal static ConcreteState absorb = new ULAbsorbSpaces();
            internal static ConcreteState ulEOL = new UnorderedListEOL();
            internal static ConcreteState ulNewLine = new UnorderedListNewLine();
            internal static ConcreteState ulNextLine = new UnorderedListNextLine();
            internal static ConcreteState postUnorderedList = new PostUnorderedList();
            internal static ConcreteState nestAbsorb = new CollectNestedLines.ULAbsorbSpaces();

            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ' ':
                        info.ChangeState(absorb);
                        NewTag(info, "ul");
                        //info.tag.SetClosingPrefix(info.prefix);
                        if (info.isNestedList())
                        {
                            info.tag.SetAppendage("");
                        }
                        // Indicate this is a 'tight' list
                        info.tag.Append('\n');
                        info.tag.NewLine();

                        // I still need to save all chars in case this is <hr/>
                        break;
                    default:
                        info.competitorFailed();
                        break;
                }
            }


            public override void GetAction(ActionSetting a)
            {
                a.leading = "*-+";
                a.action = Info.nop;
            }
        }
        public class ULAbsorbSpaces : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ' ':
                        break;
                    default:
                        info.NewChildTag();
                        info.tag.SetTagName("li");
                        info.ChangeState(UnorderedList.ulEOL);
                        info.process(c);
                        break;
                }
            }
        }
        public class UnorderedListEOL : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '\n':
                        info.ChangeState(UnorderedList.ulNewLine);
                        break;
                    default:
                        // Save all these chars in case of <hr/>
                        info.tag.Append(c);
                        break;
                }
            }
        }
        public class UnorderedListNewLine : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '*':
                    case '-':
                    case '+':
                        info.ChangeState(UnorderedList.ulNextLine);
                        break;
                    case '\n':
                        info.ChangeState(UnorderedList.postUnorderedList);
                        break;
                    case ' ':
                        info.tag.NewLine();
                        info.postNestingList = UnorderedList.ulNewLine;
                        info.ChangeState(UnorderedList.nestAbsorb);
                        break;
                    default:
                        info.tag.Append('\n');
                        info.ChangeState(UnorderedList.ulEOL);
                        info.process(c);
                        break;
                }
            }
        }
        public class UnorderedListNextLine : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ' ':
                        break;
                    default:
                        if (!info.tag.IsNested || !info.isParentLoose())
                        {
                            info.tag.NewLine();
                        }
                        info.PopChildTag();
                        info.NewChildTag();
                        info.tag.SetTagName("li");
                        info.ChangeState(UnorderedList.ulEOL);
                        info.process(c);
                        break;
                }
            }
        }

        public class PostUnorderedList : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '*':
                    case '-':
                    case '+':
                        // Should we note the leading char?
                        info.ChangeState(UnorderedList.ulNextLine);
                        // Make the list loose
                        info.setParentToLoose();
                        break;
                    case '\n':
                        break;
                    case ' ':
                        info.tag.NewLine();


                        // Make the list loose
                        info.setParentToLoose();

                        info.postNestingList = UnorderedList.ulNewLine;
                        info.ChangeState(UnorderedList.nestAbsorb);
                        break;
                    default:
                        info.tag.NewLine();
                        info.PopChildTag();
                        info.tag.SetAppendage("\n\n");
                        info.NewTag();
                        ///This is the new Success for a CompetitiveState
                        info.ResendAsLeading(c);
                        break;
                }
            }
            //private void 
            public override void terminate(Info info)
            {
                info.tag.NewLine();
            }
        }
        namespace CollectNestedLines
        {
            public class ULAbsorbSpaces : ConcreteState
            {
                internal static ConcreteState ulEOL = new UnorderedListEOL();
                internal static ConcreteState ulNewLine = new UnorderedListNewLine();
                internal static ConcreteState leading = new CollectNestedLines.LeadingChar();
                public override void process(Info info, char c)
                {
                    switch (c)
                    {
                        case ' ':
                            break;
                        default:
                            info.ChangeState(ulEOL);
                            info.process(c);
                            break;
                    }
                }
            }
            public class UnorderedListEOL : ConcreteState
            {
                public override void process(Info info, char c)
                {
                    switch (c)
                    {
                        case '\n':
                            info.ChangeState(ULAbsorbSpaces.ulNewLine);
                            break;
                        default:
                            info.tag.Append(c);
                            break;
                    }
                }
            }
            public class UnorderedListNewLine : ConcreteState
            {
                public override void process(Info info, char c)
                {
                    switch (c)
                    {
                        case ' ':

                            info.tag.NewLine();
                            //info.tag.KeepNewLine();
                            info.ChangeState(UnorderedList.nestAbsorb);
                            break;
                        default:
                            {
                                info.tag.NewLine();

                                // Use the program stack to save postNestingList
                                ConcreteState next = info.postNestingList;
                                postProcessNestedLines(info);
                                info.ChangeState(next); // UnorderedList.ulNewLine);
                                info.process(c);
                            }
                            break;
                    }
                }

                private static void postProcessNestedLines(Info info)
                {
                    // Recurse
                    int childTagSize = info.ChildTagSize();
                    Tag unorderedList = info.tag;
                    unorderedList.IsNested = true;
                    int size = unorderedList.size();
                    List<string> strings = unorderedList.TransferStrings();

                    info.PushNewLeadingState(ULAbsorbSpaces.leading);

                    foreach (string s in strings)
                    {
                        foreach (char c in s)
                        {
                            info.process(c);
                        }
                        info.process('\n');
                    }
                    info.process('\n');
                    info.terminate();

                    // 'pop' stuff off the stack
                    while (info.ChildTagSize()>childTagSize)
                    {
                        info.PopChildTag();
                    }
                    if (size<unorderedList.size())
                    {
                        //unorderedList.KeepNewLine();
                    }
                    info.PopLeading();
                }
            }
            public class LeadingChar : ConcreteState
            {
                public override void process(Info info, char c)
                {
                    bool wasDefault = info.processListsLeadingChar(c);
                    if (!wasDefault)
                    {
                        info.NewChildTag();
                    }
                }
                public override void terminate(Info info)
                {
                    info.tag.NewLine();
                }
            }
        
        }
    }
}

